package com.zc.framework.shiro.dao;

import com.zc.framework.cache.ICache;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Set;

/**
 * Created by zc on 2016/8/19.
 */
public class RedisShiroManager {

    private String cacheName;
    private CacheManager cacheManager;

    public RedisShiroManager() {
    }

    public void init() {

    }

    public ICache getCache(){
        ICache cache = null;
        if(this.cacheManager != null){
            cache = (ICache) this.cacheManager.getCache(this.getCacheName());
        }
        return cache;
    }

    public byte[] get(byte[] key) {
        Cache.ValueWrapper result = this.getCache().get(key);
        if(result != null){
            return (byte[]) result.get();
        }

        return  null;
    }

    public byte[] set(byte[] key, byte[] value) {
        this.getCache().put(key, value);
        return value;
    }

    public void del(byte[] key) {
        this.getCache().evict(key);
    }

    public void flushDB(){
        this.getCache().flushDB();
    }

    public Long dbSize() {
        return this.getCache().dbSize();
    }

    public Set<byte[]> keys(String pattern) {
        return this.getCache().keys(pattern);
    }

    public int getExpire() {
        return this.getCache().getExprie();
    }


    public String getCacheName() {
        return cacheName;
    }

    public void setCacheName(String cacheName) {
        this.cacheName = cacheName;
    }

    public CacheManager getCacheManager() {
        return cacheManager;
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
}
